<?php
/*
登录模块，包括登录用户的增删改、银行管理、中介管理
*/
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{

  public function index(){
    $this->display('Login/index');
  }

  public function DoLogin(){//判断用户名密码是否正确，ajax传前端有提示
        if(IS_POST){

            $username = I('post.username');
            $password = I('post.password');
            if(trim($username) == '' || trim($password) == ''){
                $this -> jsonReply('500','用户名或密码不能为空！');
            }
            else{
              $haveUser = D('User') -> findSameUser(trim($username));
                if($haveUser){
                    $levellist = M('level')->where('level_id='.$haveUser['level']) -> find();
                  if($haveUser['pwd'] == md5($password)){
                     $user_auth = array(
                       'username' => $haveUser['user_name'],
                       'userid' => $haveUser['user_id'],
                       'roleid'   => $haveUser['level'],
                       'banks'  => $haveUser['bank_id'],
                       'place'  => $haveUser['place'],
                       'oa_level'  => $haveUser['oa_level'],
                       'level'  => $levellist['able_ids'],
                       'leader' => $haveUser['leader']
                     );
                      session('USER_AUTH',$user_auth);
                      $this -> jsonReply('200','登录成功！');
                  }
                  else{
                      session('USER_AUTH','');
                      $this -> jsonReply('500','您的密码不正确！');
                  }
                }
                else{
                      $this -> jsonReply('500','用户不存在！');
                }
            }

        }
    }

    public function LoginOut(){//退出登录
          session('USER_AUTH',null);
          $this->redirect('Login/index');
    }

    //封装json返回函数
  protected function jsonReply($code,$message,$data=array(),$json_option = 0){
    $arr['ResultCode'] = $code;
    $arr['ResultMessage'] = $message;
    if(!empty($data)){
      $arr['ResultBody'] = $data;
    }
    exit(json_encode($arr, $json_option));
 }















}
